blob: 91f2772176ad19037fac9c341a091351e084792d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- future -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_FUTURE
12#define _LIBCPP_FUTURE
13
14/*
15 future synopsis
16
17namespace std
18{
19
20enum class future_errc
21{
Howard Hinnantcd942f12013-09-14 18:20:1022 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:1623 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:1024 no_state,
25 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:1626};
27
28enum class launch
29{
Howard Hinnant66895642010-11-23 18:33:5430 async = 1,
31 deferred = 2,
32 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:1633};
34
35enum class future_status
36{
37 ready,
38 timeout,
39 deferred
40};
41
42template <> struct is_error_code_enum<future_errc> : public true_type { };
Howard Hinnant8bf01dd2012-07-21 17:46:5543error_code make_error_code(future_errc e) noexcept;
44error_condition make_error_condition(future_errc e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1645
Howard Hinnant8bf01dd2012-07-21 17:46:5546const error_category& future_category() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1647
48class future_error
49 : public logic_error
50{
51public:
52 future_error(error_code ec); // exposition only
Marshall Clow5ec20df2016-11-14 18:56:2453 explicit future_error(future_errc); // C++17
Howard Hinnant8bf01dd2012-07-21 17:46:5554 const error_code& code() const noexcept;
55 const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1656};
57
58template <class R>
59class promise
60{
61public:
62 promise();
63 template <class Allocator>
64 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5565 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1666 promise(const promise& rhs) = delete;
67 ~promise();
68
69 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:5570 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1671 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:5572 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673
74 // retrieving the result
75 future<R> get_future();
76
77 // setting the result
78 void set_value(const R& r);
79 void set_value(R&& r);
80 void set_exception(exception_ptr p);
81
82 // setting the result with deferred notification
83 void set_value_at_thread_exit(const R& r);
84 void set_value_at_thread_exit(R&& r);
85 void set_exception_at_thread_exit(exception_ptr p);
86};
87
88template <class R>
89class promise<R&>
90{
91public:
92 promise();
93 template <class Allocator>
94 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:5595 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1696 promise(const promise& rhs) = delete;
97 ~promise();
98
99 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55100 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16101 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55102 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16103
104 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19105 future<R&> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107 // setting the result
108 void set_value(R& r);
109 void set_exception(exception_ptr p);
110
111 // setting the result with deferred notification
112 void set_value_at_thread_exit(R&);
113 void set_exception_at_thread_exit(exception_ptr p);
114};
115
116template <>
117class promise<void>
118{
119public:
120 promise();
121 template <class Allocator>
122 promise(allocator_arg_t, const Allocator& a);
Howard Hinnant8bf01dd2012-07-21 17:46:55123 promise(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16124 promise(const promise& rhs) = delete;
125 ~promise();
126
127 // assignment
Howard Hinnant8bf01dd2012-07-21 17:46:55128 promise& operator=(promise&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16129 promise& operator=(const promise& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55130 void swap(promise& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16131
132 // retrieving the result
Howard Hinnant47499b12010-08-27 20:10:19133 future<void> get_future();
Howard Hinnantbc8d3f92010-05-11 19:42:16134
135 // setting the result
136 void set_value();
137 void set_exception(exception_ptr p);
138
139 // setting the result with deferred notification
140 void set_value_at_thread_exit();
141 void set_exception_at_thread_exit(exception_ptr p);
142};
143
Howard Hinnant8bf01dd2012-07-21 17:46:55144template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16145
146template <class R, class Alloc>
147 struct uses_allocator<promise<R>, Alloc> : public true_type {};
148
149template <class R>
150class future
151{
152public:
Howard Hinnant8bf01dd2012-07-21 17:46:55153 future() noexcept;
154 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16155 future(const future& rhs) = delete;
156 ~future();
157 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55158 future& operator=(future&&) noexcept;
Marshall Clow19cd3fd2017-01-25 20:14:03159 shared_future<R> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16160
161 // retrieving the value
162 R get();
163
164 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55165 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16166
167 void wait() const;
168 template <class Rep, class Period>
169 future_status
170 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
171 template <class Clock, class Duration>
172 future_status
173 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
174};
175
176template <class R>
177class future<R&>
178{
179public:
Howard Hinnant8bf01dd2012-07-21 17:46:55180 future() noexcept;
181 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16182 future(const future& rhs) = delete;
183 ~future();
184 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55185 future& operator=(future&&) noexcept;
Marshall Clow9bb0cca2017-01-24 23:28:25186 shared_future<R&> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 // retrieving the value
189 R& get();
190
191 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55192 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16193
194 void wait() const;
195 template <class Rep, class Period>
196 future_status
197 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
198 template <class Clock, class Duration>
199 future_status
200 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
201};
202
203template <>
204class future<void>
205{
206public:
Howard Hinnant8bf01dd2012-07-21 17:46:55207 future() noexcept;
208 future(future&&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16209 future(const future& rhs) = delete;
210 ~future();
211 future& operator=(const future& rhs) = delete;
Howard Hinnant8bf01dd2012-07-21 17:46:55212 future& operator=(future&&) noexcept;
Marshall Clow9bb0cca2017-01-24 23:28:25213 shared_future<void> share() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16214
215 // retrieving the value
216 void get();
217
218 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55219 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16220
221 void wait() const;
222 template <class Rep, class Period>
223 future_status
224 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
225 template <class Clock, class Duration>
226 future_status
227 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
228};
229
230template <class R>
231class shared_future
232{
233public:
Howard Hinnant8bf01dd2012-07-21 17:46:55234 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16235 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55236 shared_future(future<R>&&) noexcept;
237 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16238 ~shared_future();
239 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55240 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16241
242 // retrieving the value
243 const R& get() const;
244
245 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55246 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16247
248 void wait() const;
249 template <class Rep, class Period>
250 future_status
251 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
252 template <class Clock, class Duration>
253 future_status
254 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
255};
256
257template <class R>
258class shared_future<R&>
259{
260public:
Howard Hinnant8bf01dd2012-07-21 17:46:55261 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16262 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55263 shared_future(future<R&>&&) noexcept;
264 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16265 ~shared_future();
266 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55267 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16268
269 // retrieving the value
270 R& get() const;
271
272 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55273 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16274
275 void wait() const;
276 template <class Rep, class Period>
277 future_status
278 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
279 template <class Clock, class Duration>
280 future_status
281 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
282};
283
284template <>
285class shared_future<void>
286{
287public:
Howard Hinnant8bf01dd2012-07-21 17:46:55288 shared_future() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16289 shared_future(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55290 shared_future(future<void>&&) noexcept;
291 shared_future(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16292 ~shared_future();
293 shared_future& operator=(const shared_future& rhs);
Howard Hinnant8bf01dd2012-07-21 17:46:55294 shared_future& operator=(shared_future&& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16295
296 // retrieving the value
297 void get() const;
298
299 // functions to check state
Howard Hinnant8bf01dd2012-07-21 17:46:55300 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16301
302 void wait() const;
303 template <class Rep, class Period>
304 future_status
305 wait_for(const chrono::duration<Rep, Period>& rel_time) const;
306 template <class Clock, class Duration>
307 future_status
308 wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
309};
310
Howard Hinnantbc8d3f92010-05-11 19:42:16311template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23312 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16313 async(F&& f, Args&&... args);
314
315template <class F, class... Args>
Howard Hinnant0836f872013-09-21 18:17:23316 future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
Howard Hinnantbc8d3f92010-05-11 19:42:16317 async(launch policy, F&& f, Args&&... args);
318
Howard Hinnantf5256e12010-05-11 21:36:01319template <class> class packaged_task; // undefined
Howard Hinnantbc8d3f92010-05-11 19:42:16320
321template <class R, class... ArgTypes>
322class packaged_task<R(ArgTypes...)>
323{
324public:
Eric Fiselier68db6cd2016-06-01 21:05:53325 typedef R result_type; // extension
Howard Hinnantbc8d3f92010-05-11 19:42:16326
327 // construction and destruction
Howard Hinnant8bf01dd2012-07-21 17:46:55328 packaged_task() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16329 template <class F>
Howard Hinnantbc8d3f92010-05-11 19:42:16330 explicit packaged_task(F&& f);
331 template <class F, class Allocator>
Marshall Clow07546f32015-06-30 14:16:49332 packaged_task(allocator_arg_t, const Allocator& a, F&& f);
Howard Hinnantbc8d3f92010-05-11 19:42:16333 ~packaged_task();
334
335 // no copy
Howard Hinnant8131a012012-07-21 19:34:12336 packaged_task(const packaged_task&) = delete;
337 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnantbc8d3f92010-05-11 19:42:16338
339 // move support
Howard Hinnant8bf01dd2012-07-21 17:46:55340 packaged_task(packaged_task&& other) noexcept;
341 packaged_task& operator=(packaged_task&& other) noexcept;
342 void swap(packaged_task& other) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16343
Howard Hinnant8bf01dd2012-07-21 17:46:55344 bool valid() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16345
346 // result retrieval
347 future<R> get_future();
348
349 // execution
350 void operator()(ArgTypes... );
351 void make_ready_at_thread_exit(ArgTypes...);
352
353 void reset();
354};
355
356template <class R>
Howard Hinnant8bf01dd2012-07-21 17:46:55357 void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16358
359template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
360
361} // std
362
363*/
364
365#include <__config>
366#include <system_error>
Howard Hinnant47499b12010-08-27 20:10:19367#include <memory>
368#include <chrono>
369#include <exception>
Howard Hinnante6e4d012010-09-03 21:46:37370#include <mutex>
Howard Hinnant47499b12010-08-27 20:10:19371#include <thread>
Howard Hinnantbc8d3f92010-05-11 19:42:16372
Howard Hinnant08e17472011-10-17 20:05:10373#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16374#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10375#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16376
Jonathan Roelofsbaed05d2014-09-05 20:28:44377#ifdef _LIBCPP_HAS_NO_THREADS
Jonathan Roelofs8d86b2e2014-09-05 19:45:05378#error <future> is not supported on this single threaded system
379#else // !_LIBCPP_HAS_NO_THREADS
380
Howard Hinnantbc8d3f92010-05-11 19:42:16381_LIBCPP_BEGIN_NAMESPACE_STD
382
383//enum class future_errc
Howard Hinnantf6d875f2011-12-02 19:36:40384_LIBCPP_DECLARE_STRONG_ENUM(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16385{
Howard Hinnantcd942f12013-09-14 18:20:10386 future_already_retrieved = 1,
Howard Hinnantbc8d3f92010-05-11 19:42:16387 promise_already_satisfied,
Howard Hinnantcd942f12013-09-14 18:20:10388 no_state,
389 broken_promise
Howard Hinnantbc8d3f92010-05-11 19:42:16390};
Howard Hinnantf6d875f2011-12-02 19:36:40391_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
Howard Hinnantbc8d3f92010-05-11 19:42:16392
Howard Hinnant8c6cbb22010-09-22 14:16:26393template <>
Eric Fiselierc3589a82017-01-04 23:56:00394struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
Howard Hinnanta6521722010-08-25 17:32:05395
Howard Hinnantf6d875f2011-12-02 19:36:40396#ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
397template <>
Eric Fiselierc3589a82017-01-04 23:56:00398struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type { };
Howard Hinnantf6d875f2011-12-02 19:36:40399#endif
400
Howard Hinnantbc8d3f92010-05-11 19:42:16401//enum class launch
Howard Hinnantf6d875f2011-12-02 19:36:40402_LIBCPP_DECLARE_STRONG_ENUM(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16403{
Howard Hinnant66895642010-11-23 18:33:54404 async = 1,
405 deferred = 2,
406 any = async | deferred
Howard Hinnantbc8d3f92010-05-11 19:42:16407};
Howard Hinnantf6d875f2011-12-02 19:36:40408_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
Howard Hinnantbc8d3f92010-05-11 19:42:16409
Howard Hinnantf491e512013-06-29 18:38:17410#ifndef _LIBCPP_HAS_NO_STRONG_ENUMS
411
412#ifdef _LIBCXX_UNDERLYING_TYPE
413typedef underlying_type<launch>::type __launch_underlying_type;
414#else
415typedef int __launch_underlying_type;
416#endif
417
418inline _LIBCPP_INLINE_VISIBILITY
419_LIBCPP_CONSTEXPR
420launch
421operator&(launch __x, launch __y)
422{
423 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) &
424 static_cast<__launch_underlying_type>(__y));
425}
426
427inline _LIBCPP_INLINE_VISIBILITY
428_LIBCPP_CONSTEXPR
429launch
430operator|(launch __x, launch __y)
431{
432 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) |
433 static_cast<__launch_underlying_type>(__y));
434}
435
436inline _LIBCPP_INLINE_VISIBILITY
437_LIBCPP_CONSTEXPR
438launch
439operator^(launch __x, launch __y)
440{
441 return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^
442 static_cast<__launch_underlying_type>(__y));
443}
444
445inline _LIBCPP_INLINE_VISIBILITY
446_LIBCPP_CONSTEXPR
447launch
448operator~(launch __x)
449{
Howard Hinnant6a683bf2013-07-02 18:01:41450 return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
Howard Hinnantf491e512013-06-29 18:38:17451}
452
453inline _LIBCPP_INLINE_VISIBILITY
454launch&
455operator&=(launch& __x, launch __y)
456{
457 __x = __x & __y; return __x;
458}
459
460inline _LIBCPP_INLINE_VISIBILITY
461launch&
462operator|=(launch& __x, launch __y)
463{
464 __x = __x | __y; return __x;
465}
466
467inline _LIBCPP_INLINE_VISIBILITY
468launch&
469operator^=(launch& __x, launch __y)
470{
471 __x = __x ^ __y; return __x;
472}
473
474#endif // !_LIBCPP_HAS_NO_STRONG_ENUMS
475
Howard Hinnantbc8d3f92010-05-11 19:42:16476//enum class future_status
Howard Hinnantf6d875f2011-12-02 19:36:40477_LIBCPP_DECLARE_STRONG_ENUM(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16478{
Howard Hinnantbc8d3f92010-05-11 19:42:16479 ready,
480 timeout,
481 deferred
482};
Howard Hinnantf6d875f2011-12-02 19:36:40483_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
Howard Hinnantbc8d3f92010-05-11 19:42:16484
Howard Hinnant83eade62013-03-06 23:30:19485_LIBCPP_FUNC_VIS
Howard Hinnant8bf01dd2012-07-21 17:46:55486const error_category& future_category() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05487
488inline _LIBCPP_INLINE_VISIBILITY
489error_code
Howard Hinnant8bf01dd2012-07-21 17:46:55490make_error_code(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05491{
492 return error_code(static_cast<int>(__e), future_category());
493}
494
495inline _LIBCPP_INLINE_VISIBILITY
496error_condition
Howard Hinnant8bf01dd2012-07-21 17:46:55497make_error_condition(future_errc __e) _NOEXCEPT
Howard Hinnanta6521722010-08-25 17:32:05498{
499 return error_condition(static_cast<int>(__e), future_category());
500}
501
Howard Hinnant8c6cbb22010-09-22 14:16:26502class _LIBCPP_EXCEPTION_ABI future_error
Howard Hinnanta6521722010-08-25 17:32:05503 : public logic_error
504{
505 error_code __ec_;
506public:
507 future_error(error_code __ec);
Marshall Clow5ec20df2016-11-14 18:56:24508#if _LIBCPP_STD_VERS > 14
509 explicit future_error(future_errc _Ev) : logic_error(), __ec_(make_error_code(_Ev)) {}
510#endif
Howard Hinnant8c6cbb22010-09-22 14:16:26511 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:55512 const error_code& code() const _NOEXCEPT {return __ec_;}
Howard Hinnantac6de542011-07-07 21:03:52513
514 virtual ~future_error() _NOEXCEPT;
Howard Hinnanta6521722010-08-25 17:32:05515};
516
Marshall Clow14c09a22016-08-25 15:09:01517_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
Eric Fiselier423ca202015-10-02 21:25:15518void __throw_future_error(future_errc _Ev)
Marshall Clowa1899742015-09-03 15:11:32519{
520#ifndef _LIBCPP_NO_EXCEPTIONS
521 throw future_error(make_error_code(_Ev));
522#else
Eric Fiselier2ab8f622016-12-24 01:56:25523 ((void)_Ev);
Marshall Clow14c09a22016-08-25 15:09:01524 _VSTD::abort();
Marshall Clowa1899742015-09-03 15:11:32525#endif
526}
527
Howard Hinnant0f678bd2013-08-12 18:38:34528class _LIBCPP_TYPE_VIS __assoc_sub_state
Howard Hinnant47499b12010-08-27 20:10:19529 : public __shared_count
530{
531protected:
532 exception_ptr __exception_;
533 mutable mutex __mut_;
534 mutable condition_variable __cv_;
535 unsigned __state_;
536
Howard Hinnant1694d232011-05-28 14:41:13537 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:21538 void __sub_wait(unique_lock<mutex>& __lk);
Howard Hinnant47499b12010-08-27 20:10:19539public:
540 enum
541 {
542 __constructed = 1,
543 __future_attached = 2,
544 ready = 4,
545 deferred = 8
546 };
547
Howard Hinnant8c6cbb22010-09-22 14:16:26548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19549 __assoc_sub_state() : __state_(0) {}
550
Howard Hinnant8c6cbb22010-09-22 14:16:26551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19552 bool __has_value() const
553 {return (__state_ & __constructed) || (__exception_ != nullptr);}
554
Howard Hinnant8c6cbb22010-09-22 14:16:26555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1b031c92013-01-14 20:01:24556 void __set_future_attached()
557 {
558 lock_guard<mutex> __lk(__mut_);
559 __state_ |= __future_attached;
560 }
Howard Hinnant8c6cbb22010-09-22 14:16:26561 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45562 bool __has_future_attached() const {return (__state_ & __future_attached) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19563
Howard Hinnant8c6cbb22010-09-22 14:16:26564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:21565 void __set_deferred() {__state_ |= deferred;}
566
Howard Hinnant47499b12010-08-27 20:10:19567 void __make_ready();
Howard Hinnant8c6cbb22010-09-22 14:16:26568 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9de3d4c2013-10-13 01:02:45569 bool __is_ready() const {return (__state_ & ready) != 0;}
Howard Hinnant47499b12010-08-27 20:10:19570
571 void set_value();
572 void set_value_at_thread_exit();
573
574 void set_exception(exception_ptr __p);
575 void set_exception_at_thread_exit(exception_ptr __p);
576
577 void copy();
578
Howard Hinnant54da3382010-08-30 18:46:21579 void wait();
Howard Hinnant47499b12010-08-27 20:10:19580 template <class _Rep, class _Period>
581 future_status
Evgeniy Stepanova3b25f82015-11-07 01:22:13582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19583 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
584 template <class _Clock, class _Duration>
585 future_status
586 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
Howard Hinnant54da3382010-08-30 18:46:21587
588 virtual void __execute();
Howard Hinnant47499b12010-08-27 20:10:19589};
590
Howard Hinnantf39daa82010-08-28 21:01:06591template <class _Clock, class _Duration>
592future_status
593__assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
594{
595 unique_lock<mutex> __lk(__mut_);
Howard Hinnant54da3382010-08-30 18:46:21596 if (__state_ & deferred)
597 return future_status::deferred;
598 while (!(__state_ & ready) && _Clock::now() < __abs_time)
Howard Hinnantf39daa82010-08-28 21:01:06599 __cv_.wait_until(__lk, __abs_time);
600 if (__state_ & ready)
601 return future_status::ready;
Howard Hinnantf39daa82010-08-28 21:01:06602 return future_status::timeout;
603}
604
605template <class _Rep, class _Period>
Evgeniy Stepanova3b25f82015-11-07 01:22:13606inline
Howard Hinnantf39daa82010-08-28 21:01:06607future_status
608__assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
609{
Howard Hinnantf8f85212010-11-20 19:16:30610 return wait_until(chrono::steady_clock::now() + __rel_time);
Howard Hinnantf39daa82010-08-28 21:01:06611}
612
Howard Hinnant99968442011-11-29 18:15:50613template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19614class __assoc_state
615 : public __assoc_sub_state
616{
617 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50618 typedef typename aligned_storage<sizeof(_Rp), alignment_of<_Rp>::value>::type _Up;
Howard Hinnant47499b12010-08-27 20:10:19619protected:
Howard Hinnant99968442011-11-29 18:15:50620 _Up __value_;
Howard Hinnant47499b12010-08-27 20:10:19621
Howard Hinnant1694d232011-05-28 14:41:13622 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19623public:
624
625 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19626#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19627 void set_value(_Arg&& __arg);
628#else
629 void set_value(_Arg& __arg);
630#endif
631
632 template <class _Arg>
Howard Hinnant73d21a42010-09-04 23:28:19633#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:19634 void set_value_at_thread_exit(_Arg&& __arg);
635#else
636 void set_value_at_thread_exit(_Arg& __arg);
637#endif
638
Howard Hinnant99968442011-11-29 18:15:50639 _Rp move();
640 typename add_lvalue_reference<_Rp>::type copy();
Howard Hinnant47499b12010-08-27 20:10:19641};
642
Howard Hinnant99968442011-11-29 18:15:50643template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19644void
Howard Hinnant99968442011-11-29 18:15:50645__assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19646{
647 if (this->__state_ & base::__constructed)
Howard Hinnant99968442011-11-29 18:15:50648 reinterpret_cast<_Rp*>(&__value_)->~_Rp();
Howard Hinnant47499b12010-08-27 20:10:19649 delete this;
650}
651
Howard Hinnant99968442011-11-29 18:15:50652template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19653template <class _Arg>
654void
Howard Hinnant73d21a42010-09-04 23:28:19655#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50656__assoc_state<_Rp>::set_value(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19657#else
Howard Hinnant99968442011-11-29 18:15:50658__assoc_state<_Rp>::set_value(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19659#endif
660{
661 unique_lock<mutex> __lk(this->__mut_);
662 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15663 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50664 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19665 this->__state_ |= base::__constructed | base::ready;
Howard Hinnant47499b12010-08-27 20:10:19666 __cv_.notify_all();
667}
668
Howard Hinnant99968442011-11-29 18:15:50669template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:19670template <class _Arg>
671void
Howard Hinnant73d21a42010-09-04 23:28:19672#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:50673__assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg)
Howard Hinnant47499b12010-08-27 20:10:19674#else
Howard Hinnant99968442011-11-29 18:15:50675__assoc_state<_Rp>::set_value_at_thread_exit(_Arg& __arg)
Howard Hinnant47499b12010-08-27 20:10:19676#endif
677{
678 unique_lock<mutex> __lk(this->__mut_);
679 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15680 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnant99968442011-11-29 18:15:50681 ::new(&__value_) _Rp(_VSTD::forward<_Arg>(__arg));
Howard Hinnant47499b12010-08-27 20:10:19682 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04683 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnant47499b12010-08-27 20:10:19684}
685
Howard Hinnant99968442011-11-29 18:15:50686template <class _Rp>
687_Rp
688__assoc_state<_Rp>::move()
Howard Hinnant47499b12010-08-27 20:10:19689{
690 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21691 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19692 if (this->__exception_ != nullptr)
693 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50694 return _VSTD::move(*reinterpret_cast<_Rp*>(&__value_));
Howard Hinnant47499b12010-08-27 20:10:19695}
696
Howard Hinnant99968442011-11-29 18:15:50697template <class _Rp>
698typename add_lvalue_reference<_Rp>::type
699__assoc_state<_Rp>::copy()
Howard Hinnant47499b12010-08-27 20:10:19700{
701 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21702 this->__sub_wait(__lk);
Howard Hinnant47499b12010-08-27 20:10:19703 if (this->__exception_ != nullptr)
704 rethrow_exception(this->__exception_);
Howard Hinnant99968442011-11-29 18:15:50705 return *reinterpret_cast<_Rp*>(&__value_);
Howard Hinnant47499b12010-08-27 20:10:19706}
707
Howard Hinnant99968442011-11-29 18:15:50708template <class _Rp>
709class __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06710 : public __assoc_sub_state
711{
712 typedef __assoc_sub_state base;
Howard Hinnant99968442011-11-29 18:15:50713 typedef _Rp* _Up;
Howard Hinnantf39daa82010-08-28 21:01:06714protected:
Howard Hinnant99968442011-11-29 18:15:50715 _Up __value_;
Howard Hinnantf39daa82010-08-28 21:01:06716
Howard Hinnant1694d232011-05-28 14:41:13717 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06718public:
719
Howard Hinnant99968442011-11-29 18:15:50720 void set_value(_Rp& __arg);
721 void set_value_at_thread_exit(_Rp& __arg);
Howard Hinnantf39daa82010-08-28 21:01:06722
Howard Hinnant99968442011-11-29 18:15:50723 _Rp& copy();
Howard Hinnantf39daa82010-08-28 21:01:06724};
725
Howard Hinnant99968442011-11-29 18:15:50726template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06727void
Howard Hinnant99968442011-11-29 18:15:50728__assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06729{
730 delete this;
731}
732
Howard Hinnant99968442011-11-29 18:15:50733template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06734void
Howard Hinnant99968442011-11-29 18:15:50735__assoc_state<_Rp&>::set_value(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06736{
737 unique_lock<mutex> __lk(this->__mut_);
738 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15739 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55740 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06741 this->__state_ |= base::__constructed | base::ready;
Howard Hinnantf39daa82010-08-28 21:01:06742 __cv_.notify_all();
743}
744
Howard Hinnant99968442011-11-29 18:15:50745template <class _Rp>
Howard Hinnantf39daa82010-08-28 21:01:06746void
Howard Hinnant99968442011-11-29 18:15:50747__assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg)
Howard Hinnantf39daa82010-08-28 21:01:06748{
749 unique_lock<mutex> __lk(this->__mut_);
750 if (this->__has_value())
Eric Fiselier423ca202015-10-02 21:25:15751 __throw_future_error(future_errc::promise_already_satisfied);
Howard Hinnanta4e87ab2013-08-08 18:38:55752 __value_ = _VSTD::addressof(__arg);
Howard Hinnantf39daa82010-08-28 21:01:06753 this->__state_ |= base::__constructed;
Howard Hinnant5306d682010-10-14 19:18:04754 __thread_local_data()->__make_ready_at_thread_exit(this);
Howard Hinnantf39daa82010-08-28 21:01:06755}
756
Howard Hinnant99968442011-11-29 18:15:50757template <class _Rp>
758_Rp&
759__assoc_state<_Rp&>::copy()
Howard Hinnantf39daa82010-08-28 21:01:06760{
761 unique_lock<mutex> __lk(this->__mut_);
Howard Hinnant54da3382010-08-30 18:46:21762 this->__sub_wait(__lk);
Howard Hinnantf39daa82010-08-28 21:01:06763 if (this->__exception_ != nullptr)
764 rethrow_exception(this->__exception_);
765 return *__value_;
766}
767
Howard Hinnant99968442011-11-29 18:15:50768template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19769class __assoc_state_alloc
Howard Hinnant99968442011-11-29 18:15:50770 : public __assoc_state<_Rp>
Howard Hinnant47499b12010-08-27 20:10:19771{
Howard Hinnant99968442011-11-29 18:15:50772 typedef __assoc_state<_Rp> base;
Howard Hinnant47499b12010-08-27 20:10:19773 _Alloc __alloc_;
774
Howard Hinnant1694d232011-05-28 14:41:13775 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19776public:
Howard Hinnant8c6cbb22010-09-22 14:16:26777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19778 explicit __assoc_state_alloc(const _Alloc& __a)
779 : __alloc_(__a) {}
780};
781
Howard Hinnant99968442011-11-29 18:15:50782template <class _Rp, class _Alloc>
Howard Hinnant47499b12010-08-27 20:10:19783void
Howard Hinnant99968442011-11-29 18:15:50784__assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19785{
786 if (this->__state_ & base::__constructed)
Howard Hinnanta4e87ab2013-08-08 18:38:55787 reinterpret_cast<_Rp*>(_VSTD::addressof(this->__value_))->~_Rp();
Eric Fiselier8492cd82015-02-05 23:01:40788 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
789 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45790 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40791 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19792 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45793 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19794}
795
Howard Hinnant99968442011-11-29 18:15:50796template <class _Rp, class _Alloc>
797class __assoc_state_alloc<_Rp&, _Alloc>
798 : public __assoc_state<_Rp&>
Howard Hinnantf39daa82010-08-28 21:01:06799{
Howard Hinnant99968442011-11-29 18:15:50800 typedef __assoc_state<_Rp&> base;
Howard Hinnantf39daa82010-08-28 21:01:06801 _Alloc __alloc_;
802
Howard Hinnant1694d232011-05-28 14:41:13803 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnantf39daa82010-08-28 21:01:06804public:
Howard Hinnant8c6cbb22010-09-22 14:16:26805 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf39daa82010-08-28 21:01:06806 explicit __assoc_state_alloc(const _Alloc& __a)
807 : __alloc_(__a) {}
808};
809
Howard Hinnant99968442011-11-29 18:15:50810template <class _Rp, class _Alloc>
Howard Hinnantf39daa82010-08-28 21:01:06811void
Howard Hinnant99968442011-11-29 18:15:50812__assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnantf39daa82010-08-28 21:01:06813{
Eric Fiselier8492cd82015-02-05 23:01:40814 typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
815 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45816 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40817 _Al __a(__alloc_);
Howard Hinnantf39daa82010-08-28 21:01:06818 this->~__assoc_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45819 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnantf39daa82010-08-28 21:01:06820}
821
Howard Hinnant47499b12010-08-27 20:10:19822template <class _Alloc>
823class __assoc_sub_state_alloc
824 : public __assoc_sub_state
825{
826 typedef __assoc_sub_state base;
827 _Alloc __alloc_;
828
Howard Hinnant1694d232011-05-28 14:41:13829 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:19830public:
Howard Hinnant8c6cbb22010-09-22 14:16:26831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:19832 explicit __assoc_sub_state_alloc(const _Alloc& __a)
833 : __alloc_(__a) {}
834};
835
836template <class _Alloc>
837void
Howard Hinnant1694d232011-05-28 14:41:13838__assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:19839{
Eric Fiselier8492cd82015-02-05 23:01:40840 typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
841 typedef allocator_traits<_Al> _ATraits;
Eric Fiselier4d2413c2014-10-23 06:24:45842 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Eric Fiselier8492cd82015-02-05 23:01:40843 _Al __a(__alloc_);
Howard Hinnant47499b12010-08-27 20:10:19844 this->~__assoc_sub_state_alloc();
Eric Fiselier4d2413c2014-10-23 06:24:45845 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant47499b12010-08-27 20:10:19846}
847
Howard Hinnant99968442011-11-29 18:15:50848template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21849class __deferred_assoc_state
Howard Hinnant99968442011-11-29 18:15:50850 : public __assoc_state<_Rp>
Howard Hinnant54da3382010-08-30 18:46:21851{
Howard Hinnant99968442011-11-29 18:15:50852 typedef __assoc_state<_Rp> base;
Howard Hinnant54da3382010-08-30 18:46:21853
Howard Hinnant99968442011-11-29 18:15:50854 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21855
856public:
Howard Hinnant73d21a42010-09-04 23:28:19857#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50859 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21860#endif
861
862 virtual void __execute();
863};
864
Howard Hinnant73d21a42010-09-04 23:28:19865#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21866
Howard Hinnant99968442011-11-29 18:15:50867template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13868inline
Howard Hinnant99968442011-11-29 18:15:50869__deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f)
870 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21871{
872 this->__set_deferred();
873}
874
Howard Hinnant73d21a42010-09-04 23:28:19875#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21876
Howard Hinnant99968442011-11-29 18:15:50877template <class _Rp, class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21878void
Howard Hinnant99968442011-11-29 18:15:50879__deferred_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21880{
881#ifndef _LIBCPP_NO_EXCEPTIONS
882 try
883 {
884#endif // _LIBCPP_NO_EXCEPTIONS
885 this->set_value(__func_());
886#ifndef _LIBCPP_NO_EXCEPTIONS
887 }
888 catch (...)
889 {
890 this->set_exception(current_exception());
891 }
892#endif // _LIBCPP_NO_EXCEPTIONS
893}
894
Howard Hinnant99968442011-11-29 18:15:50895template <class _Fp>
896class __deferred_assoc_state<void, _Fp>
Howard Hinnant54da3382010-08-30 18:46:21897 : public __assoc_sub_state
898{
899 typedef __assoc_sub_state base;
900
Howard Hinnant99968442011-11-29 18:15:50901 _Fp __func_;
Howard Hinnant54da3382010-08-30 18:46:21902
903public:
Howard Hinnant73d21a42010-09-04 23:28:19904#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50906 explicit __deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:21907#endif
908
909 virtual void __execute();
910};
911
Howard Hinnant73d21a42010-09-04 23:28:19912#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21913
Howard Hinnant99968442011-11-29 18:15:50914template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13915inline
Howard Hinnant99968442011-11-29 18:15:50916__deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f)
917 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant54da3382010-08-30 18:46:21918{
919 this->__set_deferred();
920}
921
Howard Hinnant73d21a42010-09-04 23:28:19922#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant54da3382010-08-30 18:46:21923
Howard Hinnant99968442011-11-29 18:15:50924template <class _Fp>
Howard Hinnant54da3382010-08-30 18:46:21925void
Howard Hinnant99968442011-11-29 18:15:50926__deferred_assoc_state<void, _Fp>::__execute()
Howard Hinnant54da3382010-08-30 18:46:21927{
928#ifndef _LIBCPP_NO_EXCEPTIONS
929 try
930 {
931#endif // _LIBCPP_NO_EXCEPTIONS
932 __func_();
933 this->set_value();
934#ifndef _LIBCPP_NO_EXCEPTIONS
935 }
936 catch (...)
937 {
938 this->set_exception(current_exception());
939 }
940#endif // _LIBCPP_NO_EXCEPTIONS
941}
942
Howard Hinnant99968442011-11-29 18:15:50943template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04944class __async_assoc_state
Howard Hinnant99968442011-11-29 18:15:50945 : public __assoc_state<_Rp>
Howard Hinnant57cff292011-05-19 15:05:04946{
Howard Hinnant99968442011-11-29 18:15:50947 typedef __assoc_state<_Rp> base;
Howard Hinnant57cff292011-05-19 15:05:04948
Howard Hinnant99968442011-11-29 18:15:50949 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:04950
Howard Hinnant1694d232011-05-28 14:41:13951 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:04952public:
953#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:13954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50955 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:04956#endif
957
958 virtual void __execute();
959};
960
961#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
962
Howard Hinnant99968442011-11-29 18:15:50963template <class _Rp, class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:13964inline
Howard Hinnant99968442011-11-29 18:15:50965__async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f)
966 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:04967{
968}
969
970#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
971
Howard Hinnant99968442011-11-29 18:15:50972template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04973void
Howard Hinnant99968442011-11-29 18:15:50974__async_assoc_state<_Rp, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:04975{
976#ifndef _LIBCPP_NO_EXCEPTIONS
977 try
978 {
979#endif // _LIBCPP_NO_EXCEPTIONS
980 this->set_value(__func_());
981#ifndef _LIBCPP_NO_EXCEPTIONS
982 }
983 catch (...)
984 {
985 this->set_exception(current_exception());
986 }
987#endif // _LIBCPP_NO_EXCEPTIONS
988}
989
Howard Hinnant99968442011-11-29 18:15:50990template <class _Rp, class _Fp>
Howard Hinnant57cff292011-05-19 15:05:04991void
Howard Hinnant99968442011-11-29 18:15:50992__async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:04993{
994 this->wait();
995 base::__on_zero_shared();
996}
997
Howard Hinnant99968442011-11-29 18:15:50998template <class _Fp>
999class __async_assoc_state<void, _Fp>
Howard Hinnant57cff292011-05-19 15:05:041000 : public __assoc_sub_state
1001{
1002 typedef __assoc_sub_state base;
1003
Howard Hinnant99968442011-11-29 18:15:501004 _Fp __func_;
Howard Hinnant57cff292011-05-19 15:05:041005
Howard Hinnant1694d232011-05-28 14:41:131006 virtual void __on_zero_shared() _NOEXCEPT;
Howard Hinnant57cff292011-05-19 15:05:041007public:
1008#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Evgeniy Stepanova3b25f82015-11-07 01:22:131009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501010 explicit __async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041011#endif
1012
1013 virtual void __execute();
1014};
1015
1016#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1017
Howard Hinnant99968442011-11-29 18:15:501018template <class _Fp>
Evgeniy Stepanova3b25f82015-11-07 01:22:131019inline
Howard Hinnant99968442011-11-29 18:15:501020__async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f)
1021 : __func_(_VSTD::forward<_Fp>(__f))
Howard Hinnant57cff292011-05-19 15:05:041022{
1023}
1024
1025#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1026
Howard Hinnant99968442011-11-29 18:15:501027template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041028void
Howard Hinnant99968442011-11-29 18:15:501029__async_assoc_state<void, _Fp>::__execute()
Howard Hinnant57cff292011-05-19 15:05:041030{
1031#ifndef _LIBCPP_NO_EXCEPTIONS
1032 try
1033 {
1034#endif // _LIBCPP_NO_EXCEPTIONS
1035 __func_();
1036 this->set_value();
1037#ifndef _LIBCPP_NO_EXCEPTIONS
1038 }
1039 catch (...)
1040 {
1041 this->set_exception(current_exception());
1042 }
1043#endif // _LIBCPP_NO_EXCEPTIONS
1044}
1045
Howard Hinnant99968442011-11-29 18:15:501046template <class _Fp>
Howard Hinnant57cff292011-05-19 15:05:041047void
Howard Hinnant99968442011-11-29 18:15:501048__async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT
Howard Hinnant57cff292011-05-19 15:05:041049{
1050 this->wait();
1051 base::__on_zero_shared();
1052}
1053
Eric Fiselierc3589a82017-01-04 23:56:001054template <class _Rp> class _LIBCPP_TEMPLATE_VIS promise;
1055template <class _Rp> class _LIBCPP_TEMPLATE_VIS shared_future;
Howard Hinnant47499b12010-08-27 20:10:191056
1057// future
1058
Eric Fiselierc3589a82017-01-04 23:56:001059template <class _Rp> class _LIBCPP_TEMPLATE_VIS future;
Howard Hinnant54da3382010-08-30 18:46:211060
Howard Hinnant99968442011-11-29 18:15:501061template <class _Rp, class _Fp>
1062future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:191063#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501064__make_deferred_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211065#else
Howard Hinnant99968442011-11-29 18:15:501066__make_deferred_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211067#endif
1068
Howard Hinnant99968442011-11-29 18:15:501069template <class _Rp, class _Fp>
1070future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:041071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501072__make_async_assoc_state(_Fp&& __f);
Howard Hinnant57cff292011-05-19 15:05:041073#else
Howard Hinnant99968442011-11-29 18:15:501074__make_async_assoc_state(_Fp __f);
Howard Hinnant57cff292011-05-19 15:05:041075#endif
1076
Howard Hinnant99968442011-11-29 18:15:501077template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:001078class _LIBCPP_TEMPLATE_VIS future
Howard Hinnant47499b12010-08-27 20:10:191079{
Howard Hinnant99968442011-11-29 18:15:501080 __assoc_state<_Rp>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191081
Howard Hinnant99968442011-11-29 18:15:501082 explicit future(__assoc_state<_Rp>* __state);
Howard Hinnant47499b12010-08-27 20:10:191083
1084 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251085 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211086
Howard Hinnant73d21a42010-09-04 23:28:191087#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501088 template <class _R1, class _Fp>
1089 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1090 template <class _R1, class _Fp>
1091 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211092#else
Howard Hinnant99968442011-11-29 18:15:501093 template <class _R1, class _Fp>
1094 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1095 template <class _R1, class _Fp>
1096 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211097#endif
1098
Howard Hinnant47499b12010-08-27 20:10:191099public:
Howard Hinnant8c6cbb22010-09-22 14:16:261100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551101 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191102#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551104 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191105 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1106 future(const future&) = delete;
1107 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551109 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191110 {
1111 future(std::move(__rhs)).swap(*this);
1112 return *this;
1113 }
Howard Hinnant73d21a42010-09-04 23:28:191114#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191115private:
1116 future(const future&);
1117 future& operator=(const future&);
1118public:
Howard Hinnant73d21a42010-09-04 23:28:191119#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191120 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131121 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251122 shared_future<_Rp> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191123
1124 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501125 _Rp get();
Howard Hinnant47499b12010-08-27 20:10:191126
Howard Hinnant8c6cbb22010-09-22 14:16:261127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551128 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191129
1130 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551132 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191133
Howard Hinnant8c6cbb22010-09-22 14:16:261134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191135 void wait() const {__state_->wait();}
1136 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191138 future_status
1139 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1140 {return __state_->wait_for(__rel_time);}
1141 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191143 future_status
1144 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1145 {return __state_->wait_until(__abs_time);}
1146};
1147
Howard Hinnant99968442011-11-29 18:15:501148template <class _Rp>
1149future<_Rp>::future(__assoc_state<_Rp>* __state)
Howard Hinnant47499b12010-08-27 20:10:191150 : __state_(__state)
1151{
1152 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151153 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191154 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211155 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191156}
1157
Howard Hinnant54da3382010-08-30 18:46:211158struct __release_shared_count
1159{
1160 void operator()(__shared_count* p) {p->__release_shared();}
1161};
1162
Howard Hinnant99968442011-11-29 18:15:501163template <class _Rp>
1164future<_Rp>::~future()
Howard Hinnant47499b12010-08-27 20:10:191165{
1166 if (__state_)
1167 __state_->__release_shared();
1168}
1169
Howard Hinnant99968442011-11-29 18:15:501170template <class _Rp>
1171_Rp
1172future<_Rp>::get()
Howard Hinnant47499b12010-08-27 20:10:191173{
Howard Hinnant54da3382010-08-30 18:46:211174 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501175 __assoc_state<_Rp>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191176 __state_ = nullptr;
1177 return __s->move();
1178}
1179
Howard Hinnant99968442011-11-29 18:15:501180template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:001181class _LIBCPP_TEMPLATE_VIS future<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191182{
Howard Hinnant99968442011-11-29 18:15:501183 __assoc_state<_Rp&>* __state_;
Howard Hinnant47499b12010-08-27 20:10:191184
Howard Hinnant99968442011-11-29 18:15:501185 explicit future(__assoc_state<_Rp&>* __state);
Howard Hinnant47499b12010-08-27 20:10:191186
1187 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251188 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211189
Howard Hinnant73d21a42010-09-04 23:28:191190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501191 template <class _R1, class _Fp>
1192 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1193 template <class _R1, class _Fp>
1194 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211195#else
Howard Hinnant99968442011-11-29 18:15:501196 template <class _R1, class _Fp>
1197 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1198 template <class _R1, class _Fp>
1199 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211200#endif
1201
Howard Hinnant47499b12010-08-27 20:10:191202public:
Howard Hinnant8c6cbb22010-09-22 14:16:261203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551204 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191205#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551207 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191208 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1209 future(const future&) = delete;
1210 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551212 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191213 {
1214 future(std::move(__rhs)).swap(*this);
1215 return *this;
1216 }
Howard Hinnant73d21a42010-09-04 23:28:191217#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191218private:
1219 future(const future&);
1220 future& operator=(const future&);
1221public:
Howard Hinnant73d21a42010-09-04 23:28:191222#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191223 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131224 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251225 shared_future<_Rp&> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191226
1227 // retrieving the value
Howard Hinnant99968442011-11-29 18:15:501228 _Rp& get();
Howard Hinnant47499b12010-08-27 20:10:191229
Howard Hinnant8c6cbb22010-09-22 14:16:261230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551231 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191232
1233 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551235 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191236
Howard Hinnant8c6cbb22010-09-22 14:16:261237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191238 void wait() const {__state_->wait();}
1239 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191241 future_status
1242 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1243 {return __state_->wait_for(__rel_time);}
1244 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191246 future_status
1247 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1248 {return __state_->wait_until(__abs_time);}
1249};
1250
Howard Hinnant99968442011-11-29 18:15:501251template <class _Rp>
1252future<_Rp&>::future(__assoc_state<_Rp&>* __state)
Howard Hinnant47499b12010-08-27 20:10:191253 : __state_(__state)
1254{
1255 if (__state_->__has_future_attached())
Eric Fiselier423ca202015-10-02 21:25:151256 __throw_future_error(future_errc::future_already_retrieved);
Howard Hinnant47499b12010-08-27 20:10:191257 __state_->__add_shared();
Howard Hinnant54da3382010-08-30 18:46:211258 __state_->__set_future_attached();
Howard Hinnant47499b12010-08-27 20:10:191259}
1260
Howard Hinnant99968442011-11-29 18:15:501261template <class _Rp>
1262future<_Rp&>::~future()
Howard Hinnant47499b12010-08-27 20:10:191263{
1264 if (__state_)
1265 __state_->__release_shared();
1266}
1267
Howard Hinnant99968442011-11-29 18:15:501268template <class _Rp>
1269_Rp&
1270future<_Rp&>::get()
Howard Hinnant47499b12010-08-27 20:10:191271{
Howard Hinnant54da3382010-08-30 18:46:211272 unique_ptr<__shared_count, __release_shared_count> __(__state_);
Howard Hinnant99968442011-11-29 18:15:501273 __assoc_state<_Rp&>* __s = __state_;
Howard Hinnant47499b12010-08-27 20:10:191274 __state_ = nullptr;
1275 return __s->copy();
1276}
1277
1278template <>
Howard Hinnant83eade62013-03-06 23:30:191279class _LIBCPP_TYPE_VIS future<void>
Howard Hinnant47499b12010-08-27 20:10:191280{
1281 __assoc_sub_state* __state_;
1282
1283 explicit future(__assoc_sub_state* __state);
1284
1285 template <class> friend class promise;
Howard Hinnant99be8232010-09-03 18:39:251286 template <class> friend class shared_future;
Howard Hinnant54da3382010-08-30 18:46:211287
Howard Hinnant73d21a42010-09-04 23:28:191288#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501289 template <class _R1, class _Fp>
1290 friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
1291 template <class _R1, class _Fp>
1292 friend future<_R1> __make_async_assoc_state(_Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211293#else
Howard Hinnant99968442011-11-29 18:15:501294 template <class _R1, class _Fp>
1295 friend future<_R1> __make_deferred_assoc_state(_Fp __f);
1296 template <class _R1, class _Fp>
1297 friend future<_R1> __make_async_assoc_state(_Fp __f);
Howard Hinnant54da3382010-08-30 18:46:211298#endif
1299
Howard Hinnant47499b12010-08-27 20:10:191300public:
Howard Hinnant8c6cbb22010-09-22 14:16:261301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551302 future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant73d21a42010-09-04 23:28:191303#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551305 future(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191306 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1307 future(const future&) = delete;
1308 future& operator=(const future&) = delete;
Howard Hinnant8c6cbb22010-09-22 14:16:261309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551310 future& operator=(future&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191311 {
1312 future(std::move(__rhs)).swap(*this);
1313 return *this;
1314 }
Howard Hinnant73d21a42010-09-04 23:28:191315#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191316private:
1317 future(const future&);
1318 future& operator=(const future&);
1319public:
Howard Hinnant73d21a42010-09-04 23:28:191320#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191321 ~future();
Evgeniy Stepanova3b25f82015-11-07 01:22:131322 _LIBCPP_INLINE_VISIBILITY
Marshall Clow9bb0cca2017-01-24 23:28:251323 shared_future<void> share() _NOEXCEPT;
Howard Hinnant47499b12010-08-27 20:10:191324
1325 // retrieving the value
1326 void get();
1327
Howard Hinnant8c6cbb22010-09-22 14:16:261328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551329 void swap(future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191330
1331 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:261332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551333 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant47499b12010-08-27 20:10:191334
Howard Hinnant8c6cbb22010-09-22 14:16:261335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191336 void wait() const {__state_->wait();}
1337 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:261338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191339 future_status
1340 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
1341 {return __state_->wait_for(__rel_time);}
1342 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:261343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant47499b12010-08-27 20:10:191344 future_status
1345 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
1346 {return __state_->wait_until(__abs_time);}
1347};
1348
Howard Hinnant99968442011-11-29 18:15:501349template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:251350inline _LIBCPP_INLINE_VISIBILITY
1351void
Howard Hinnant8bf01dd2012-07-21 17:46:551352swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:251353{
1354 __x.swap(__y);
1355}
1356
Howard Hinnant47499b12010-08-27 20:10:191357// promise<R>
1358
Howard Hinnant2b1b2d42011-06-14 19:58:171359template <class _Callable> class packaged_task;
Howard Hinnant54da3382010-08-30 18:46:211360
Howard Hinnant99968442011-11-29 18:15:501361template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:001362class _LIBCPP_TEMPLATE_VIS promise
Howard Hinnant47499b12010-08-27 20:10:191363{
Howard Hinnant99968442011-11-29 18:15:501364 __assoc_state<_Rp>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211365
Howard Hinnant8c6cbb22010-09-22 14:16:261366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551367 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211368
1369 template <class> friend class packaged_task;
Howard Hinnant47499b12010-08-27 20:10:191370public:
1371 promise();
1372 template <class _Alloc>
1373 promise(allocator_arg_t, const _Alloc& __a);
Howard Hinnant73d21a42010-09-04 23:28:191374#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551376 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191377 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1378 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191379#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191380private:
1381 promise(const promise& __rhs);
1382public:
Howard Hinnant73d21a42010-09-04 23:28:191383#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191384 ~promise();
1385
1386 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191387#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551389 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191390 {
1391 promise(std::move(__rhs)).swap(*this);
1392 return *this;
1393 }
1394 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191395#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191396private:
1397 promise& operator=(const promise& __rhs);
1398public:
Howard Hinnant73d21a42010-09-04 23:28:191399#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551401 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191402
1403 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501404 future<_Rp> get_future();
Howard Hinnant47499b12010-08-27 20:10:191405
1406 // setting the result
Howard Hinnant99968442011-11-29 18:15:501407 void set_value(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191408#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501409 void set_value(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191410#endif
1411 void set_exception(exception_ptr __p);
1412
1413 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501414 void set_value_at_thread_exit(const _Rp& __r);
Howard Hinnant73d21a42010-09-04 23:28:191415#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:501416 void set_value_at_thread_exit(_Rp&& __r);
Howard Hinnant47499b12010-08-27 20:10:191417#endif
1418 void set_exception_at_thread_exit(exception_ptr __p);
1419};
1420
Howard Hinnant99968442011-11-29 18:15:501421template <class _Rp>
1422promise<_Rp>::promise()
1423 : __state_(new __assoc_state<_Rp>)
Howard Hinnant47499b12010-08-27 20:10:191424{
1425}
1426
Howard Hinnant99968442011-11-29 18:15:501427template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191428template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501429promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191430{
Eric Fiselier4d2413c2014-10-23 06:24:451431 typedef __assoc_state_alloc<_Rp, _Alloc> _State;
1432 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191433 typedef __allocator_destructor<_A2> _D2;
1434 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451435 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1436 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1437 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191438}
1439
Howard Hinnant99968442011-11-29 18:15:501440template <class _Rp>
1441promise<_Rp>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191442{
1443 if (__state_)
1444 {
1445 if (!__state_->__has_value() && __state_->use_count() > 1)
1446 __state_->set_exception(make_exception_ptr(
1447 future_error(make_error_code(future_errc::broken_promise))
1448 ));
1449 __state_->__release_shared();
1450 }
1451}
1452
Howard Hinnant99968442011-11-29 18:15:501453template <class _Rp>
1454future<_Rp>
1455promise<_Rp>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191456{
1457 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151458 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:501459 return future<_Rp>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191460}
1461
Howard Hinnant99968442011-11-29 18:15:501462template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191463void
Howard Hinnant99968442011-11-29 18:15:501464promise<_Rp>::set_value(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191465{
1466 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151467 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191468 __state_->set_value(__r);
1469}
1470
Howard Hinnant73d21a42010-09-04 23:28:191471#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191472
Howard Hinnant99968442011-11-29 18:15:501473template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191474void
Howard Hinnant99968442011-11-29 18:15:501475promise<_Rp>::set_value(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191476{
1477 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151478 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191479 __state_->set_value(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191480}
1481
Howard Hinnant73d21a42010-09-04 23:28:191482#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191483
Howard Hinnant99968442011-11-29 18:15:501484template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191485void
Howard Hinnant99968442011-11-29 18:15:501486promise<_Rp>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191487{
Marshall Cloweaba7bb2016-05-16 16:55:321488 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191489 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151490 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191491 __state_->set_exception(__p);
1492}
1493
Howard Hinnant99968442011-11-29 18:15:501494template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191495void
Howard Hinnant99968442011-11-29 18:15:501496promise<_Rp>::set_value_at_thread_exit(const _Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191497{
1498 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151499 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191500 __state_->set_value_at_thread_exit(__r);
1501}
1502
Howard Hinnant73d21a42010-09-04 23:28:191503#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191504
Howard Hinnant99968442011-11-29 18:15:501505template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191506void
Howard Hinnant99968442011-11-29 18:15:501507promise<_Rp>::set_value_at_thread_exit(_Rp&& __r)
Howard Hinnant47499b12010-08-27 20:10:191508{
1509 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151510 __throw_future_error(future_errc::no_state);
Howard Hinnant0949eed2011-06-30 21:18:191511 __state_->set_value_at_thread_exit(_VSTD::move(__r));
Howard Hinnant47499b12010-08-27 20:10:191512}
1513
Howard Hinnant73d21a42010-09-04 23:28:191514#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191515
Howard Hinnant99968442011-11-29 18:15:501516template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191517void
Howard Hinnant99968442011-11-29 18:15:501518promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191519{
Eric Fiselierb169bb02016-05-31 01:50:551520 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191521 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151522 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191523 __state_->set_exception_at_thread_exit(__p);
1524}
1525
1526// promise<R&>
1527
Howard Hinnant99968442011-11-29 18:15:501528template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:001529class _LIBCPP_TEMPLATE_VIS promise<_Rp&>
Howard Hinnant47499b12010-08-27 20:10:191530{
Howard Hinnant99968442011-11-29 18:15:501531 __assoc_state<_Rp&>* __state_;
Howard Hinnant54da3382010-08-30 18:46:211532
Howard Hinnant8c6cbb22010-09-22 14:16:261533 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551534 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211535
1536 template <class> friend class packaged_task;
1537
Howard Hinnant47499b12010-08-27 20:10:191538public:
1539 promise();
1540 template <class _Allocator>
1541 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191542#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551544 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191545 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1546 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191547#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191548private:
1549 promise(const promise& __rhs);
1550public:
Howard Hinnant73d21a42010-09-04 23:28:191551#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191552 ~promise();
1553
1554 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191555#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551557 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191558 {
1559 promise(std::move(__rhs)).swap(*this);
1560 return *this;
1561 }
1562 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191563#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191564private:
1565 promise& operator=(const promise& __rhs);
1566public:
Howard Hinnant73d21a42010-09-04 23:28:191567#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551569 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191570
1571 // retrieving the result
Howard Hinnant99968442011-11-29 18:15:501572 future<_Rp&> get_future();
Howard Hinnant47499b12010-08-27 20:10:191573
1574 // setting the result
Howard Hinnant99968442011-11-29 18:15:501575 void set_value(_Rp& __r);
Howard Hinnant47499b12010-08-27 20:10:191576 void set_exception(exception_ptr __p);
1577
1578 // setting the result with deferred notification
Howard Hinnant99968442011-11-29 18:15:501579 void set_value_at_thread_exit(_Rp&);
Howard Hinnant47499b12010-08-27 20:10:191580 void set_exception_at_thread_exit(exception_ptr __p);
1581};
1582
Howard Hinnant99968442011-11-29 18:15:501583template <class _Rp>
1584promise<_Rp&>::promise()
1585 : __state_(new __assoc_state<_Rp&>)
Howard Hinnant47499b12010-08-27 20:10:191586{
1587}
1588
Howard Hinnant99968442011-11-29 18:15:501589template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191590template <class _Alloc>
Howard Hinnant99968442011-11-29 18:15:501591promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0)
Howard Hinnant47499b12010-08-27 20:10:191592{
Eric Fiselier4d2413c2014-10-23 06:24:451593 typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
1594 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191595 typedef __allocator_destructor<_A2> _D2;
1596 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451597 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1598 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1599 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191600}
1601
Howard Hinnant99968442011-11-29 18:15:501602template <class _Rp>
1603promise<_Rp&>::~promise()
Howard Hinnant47499b12010-08-27 20:10:191604{
1605 if (__state_)
1606 {
1607 if (!__state_->__has_value() && __state_->use_count() > 1)
1608 __state_->set_exception(make_exception_ptr(
1609 future_error(make_error_code(future_errc::broken_promise))
1610 ));
1611 __state_->__release_shared();
1612 }
1613}
1614
Howard Hinnant99968442011-11-29 18:15:501615template <class _Rp>
1616future<_Rp&>
1617promise<_Rp&>::get_future()
Howard Hinnant47499b12010-08-27 20:10:191618{
1619 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151620 __throw_future_error(future_errc::no_state);
Howard Hinnant99968442011-11-29 18:15:501621 return future<_Rp&>(__state_);
Howard Hinnant47499b12010-08-27 20:10:191622}
1623
Howard Hinnant99968442011-11-29 18:15:501624template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191625void
Howard Hinnant99968442011-11-29 18:15:501626promise<_Rp&>::set_value(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191627{
1628 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151629 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191630 __state_->set_value(__r);
1631}
1632
Howard Hinnant99968442011-11-29 18:15:501633template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191634void
Howard Hinnant99968442011-11-29 18:15:501635promise<_Rp&>::set_exception(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191636{
Marshall Cloweaba7bb2016-05-16 16:55:321637 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191638 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151639 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191640 __state_->set_exception(__p);
1641}
1642
Howard Hinnant99968442011-11-29 18:15:501643template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191644void
Howard Hinnant99968442011-11-29 18:15:501645promise<_Rp&>::set_value_at_thread_exit(_Rp& __r)
Howard Hinnant47499b12010-08-27 20:10:191646{
1647 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151648 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191649 __state_->set_value_at_thread_exit(__r);
1650}
1651
Howard Hinnant99968442011-11-29 18:15:501652template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191653void
Howard Hinnant99968442011-11-29 18:15:501654promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p)
Howard Hinnant47499b12010-08-27 20:10:191655{
Eric Fiselierb169bb02016-05-31 01:50:551656 _LIBCPP_ASSERT( __p != nullptr, "promise::set_exception_at_thread_exit: received nullptr" );
Howard Hinnant47499b12010-08-27 20:10:191657 if (__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:151658 __throw_future_error(future_errc::no_state);
Howard Hinnant47499b12010-08-27 20:10:191659 __state_->set_exception_at_thread_exit(__p);
1660}
1661
1662// promise<void>
1663
1664template <>
Howard Hinnant83eade62013-03-06 23:30:191665class _LIBCPP_TYPE_VIS promise<void>
Howard Hinnant47499b12010-08-27 20:10:191666{
1667 __assoc_sub_state* __state_;
Howard Hinnant54da3382010-08-30 18:46:211668
Howard Hinnant8c6cbb22010-09-22 14:16:261669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551670 explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant54da3382010-08-30 18:46:211671
1672 template <class> friend class packaged_task;
1673
Howard Hinnant47499b12010-08-27 20:10:191674public:
1675 promise();
1676 template <class _Allocator>
1677 promise(allocator_arg_t, const _Allocator& __a);
Howard Hinnant73d21a42010-09-04 23:28:191678#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551680 promise(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191681 : __state_(__rhs.__state_) {__rhs.__state_ = nullptr;}
1682 promise(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191683#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191684private:
1685 promise(const promise& __rhs);
1686public:
Howard Hinnant73d21a42010-09-04 23:28:191687#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191688 ~promise();
1689
1690 // assignment
Howard Hinnant73d21a42010-09-04 23:28:191691#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551693 promise& operator=(promise&& __rhs) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191694 {
1695 promise(std::move(__rhs)).swap(*this);
1696 return *this;
1697 }
1698 promise& operator=(const promise& __rhs) = delete;
Howard Hinnant73d21a42010-09-04 23:28:191699#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant47499b12010-08-27 20:10:191700private:
1701 promise& operator=(const promise& __rhs);
1702public:
Howard Hinnant73d21a42010-09-04 23:28:191703#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:261704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551705 void swap(promise& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant47499b12010-08-27 20:10:191706
1707 // retrieving the result
1708 future<void> get_future();
1709
1710 // setting the result
1711 void set_value();
1712 void set_exception(exception_ptr __p);
1713
1714 // setting the result with deferred notification
1715 void set_value_at_thread_exit();
1716 void set_exception_at_thread_exit(exception_ptr __p);
1717};
1718
1719template <class _Alloc>
1720promise<void>::promise(allocator_arg_t, const _Alloc& __a0)
1721{
Eric Fiselier4d2413c2014-10-23 06:24:451722 typedef __assoc_sub_state_alloc<_Alloc> _State;
1723 typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
Howard Hinnant47499b12010-08-27 20:10:191724 typedef __allocator_destructor<_A2> _D2;
1725 _A2 __a(__a0);
Eric Fiselier4d2413c2014-10-23 06:24:451726 unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
1727 ::new(static_cast<void*>(_VSTD::addressof(*__hold.get()))) _State(__a0);
1728 __state_ = _VSTD::addressof(*__hold.release());
Howard Hinnant47499b12010-08-27 20:10:191729}
1730
Howard Hinnant99968442011-11-29 18:15:501731template <class _Rp>
Howard Hinnant47499b12010-08-27 20:10:191732inline _LIBCPP_INLINE_VISIBILITY
1733void
Howard Hinnant8bf01dd2012-07-21 17:46:551734swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT
Howard Hinnant47499b12010-08-27 20:10:191735{
1736 __x.swap(__y);
1737}
1738
Howard Hinnant99968442011-11-29 18:15:501739template <class _Rp, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:001740 struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:261741 : public true_type {};
Howard Hinnant47499b12010-08-27 20:10:191742
Howard Hinnant54da3382010-08-30 18:46:211743#ifndef _LIBCPP_HAS_NO_VARIADICS
1744
1745// packaged_task
1746
1747template<class _Fp> class __packaged_task_base;
1748
Howard Hinnant99968442011-11-29 18:15:501749template<class _Rp, class ..._ArgTypes>
1750class __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211751{
1752 __packaged_task_base(const __packaged_task_base&);
1753 __packaged_task_base& operator=(const __packaged_task_base&);
1754public:
Howard Hinnant8c6cbb22010-09-22 14:16:261755 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211756 __packaged_task_base() {}
Howard Hinnant8c6cbb22010-09-22 14:16:261757 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:211758 virtual ~__packaged_task_base() {}
Howard Hinnant8bf01dd2012-07-21 17:46:551759 virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
Howard Hinnant54da3382010-08-30 18:46:211760 virtual void destroy() = 0;
1761 virtual void destroy_deallocate() = 0;
Howard Hinnant99968442011-11-29 18:15:501762 virtual _Rp operator()(_ArgTypes&& ...) = 0;
Howard Hinnant54da3382010-08-30 18:46:211763};
1764
1765template<class _FD, class _Alloc, class _FB> class __packaged_task_func;
1766
Howard Hinnant99968442011-11-29 18:15:501767template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1768class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>
1769 : public __packaged_task_base<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211770{
Howard Hinnant99968442011-11-29 18:15:501771 __compressed_pair<_Fp, _Alloc> __f_;
Howard Hinnant54da3382010-08-30 18:46:211772public:
Howard Hinnant8c6cbb22010-09-22 14:16:261773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501774 explicit __packaged_task_func(const _Fp& __f) : __f_(__f) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501776 explicit __packaged_task_func(_Fp&& __f) : __f_(_VSTD::move(__f)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501778 __packaged_task_func(const _Fp& __f, const _Alloc& __a)
Howard Hinnant54da3382010-08-30 18:46:211779 : __f_(__f, __a) {}
Howard Hinnant8c6cbb22010-09-22 14:16:261780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501781 __packaged_task_func(_Fp&& __f, const _Alloc& __a)
Howard Hinnant0949eed2011-06-30 21:18:191782 : __f_(_VSTD::move(__f), __a) {}
Howard Hinnant8bf01dd2012-07-21 17:46:551783 virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211784 virtual void destroy();
1785 virtual void destroy_deallocate();
Howard Hinnant99968442011-11-29 18:15:501786 virtual _Rp operator()(_ArgTypes&& ... __args);
Howard Hinnant54da3382010-08-30 18:46:211787};
1788
Howard Hinnant99968442011-11-29 18:15:501789template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211790void
Howard Hinnant99968442011-11-29 18:15:501791__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
Howard Hinnant8bf01dd2012-07-21 17:46:551792 __packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211793{
Howard Hinnant0949eed2011-06-30 21:18:191794 ::new (__p) __packaged_task_func(_VSTD::move(__f_.first()), _VSTD::move(__f_.second()));
Howard Hinnant54da3382010-08-30 18:46:211795}
1796
Howard Hinnant99968442011-11-29 18:15:501797template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211798void
Howard Hinnant99968442011-11-29 18:15:501799__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy()
Howard Hinnant54da3382010-08-30 18:46:211800{
Howard Hinnant99968442011-11-29 18:15:501801 __f_.~__compressed_pair<_Fp, _Alloc>();
Howard Hinnant54da3382010-08-30 18:46:211802}
1803
Howard Hinnant99968442011-11-29 18:15:501804template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211805void
Howard Hinnant99968442011-11-29 18:15:501806__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate()
Howard Hinnant54da3382010-08-30 18:46:211807{
Eric Fiselier4d2413c2014-10-23 06:24:451808 typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
1809 typedef allocator_traits<_Ap> _ATraits;
1810 typedef pointer_traits<typename _ATraits::pointer> _PTraits;
Howard Hinnant99968442011-11-29 18:15:501811 _Ap __a(__f_.second());
1812 __f_.~__compressed_pair<_Fp, _Alloc>();
Eric Fiselier4d2413c2014-10-23 06:24:451813 __a.deallocate(_PTraits::pointer_to(*this), 1);
Howard Hinnant54da3382010-08-30 18:46:211814}
1815
Howard Hinnant99968442011-11-29 18:15:501816template<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
1817_Rp
1818__packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
Howard Hinnant54da3382010-08-30 18:46:211819{
Howard Hinnant0949eed2011-06-30 21:18:191820 return __invoke(__f_.first(), _VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211821}
1822
Howard Hinnant2b1b2d42011-06-14 19:58:171823template <class _Callable> class __packaged_task_function;
Howard Hinnant54da3382010-08-30 18:46:211824
Howard Hinnant99968442011-11-29 18:15:501825template<class _Rp, class ..._ArgTypes>
1826class __packaged_task_function<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:211827{
Howard Hinnant99968442011-11-29 18:15:501828 typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
Howard Hinnant78f0de22013-01-21 17:26:551829 typename aligned_storage<3*sizeof(void*)>::type __buf_;
Howard Hinnant54da3382010-08-30 18:46:211830 __base* __f_;
1831
1832public:
Howard Hinnant99968442011-11-29 18:15:501833 typedef _Rp result_type;
Howard Hinnant54da3382010-08-30 18:46:211834
1835 // construct/copy/destroy:
Howard Hinnant8c6cbb22010-09-22 14:16:261836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:551837 __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
Howard Hinnant99968442011-11-29 18:15:501838 template<class _Fp>
1839 __packaged_task_function(_Fp&& __f);
1840 template<class _Fp, class _Alloc>
1841 __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
Howard Hinnant54da3382010-08-30 18:46:211842
Howard Hinnant8bf01dd2012-07-21 17:46:551843 __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
1844 __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211845
1846 __packaged_task_function(const __packaged_task_function&) = delete;
1847 __packaged_task_function& operator=(const __packaged_task_function&) = delete;
1848
1849 ~__packaged_task_function();
1850
Howard Hinnant8bf01dd2012-07-21 17:46:551851 void swap(__packaged_task_function&) _NOEXCEPT;
Howard Hinnant54da3382010-08-30 18:46:211852
Evgeniy Stepanova3b25f82015-11-07 01:22:131853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:501854 _Rp operator()(_ArgTypes...) const;
Howard Hinnant54da3382010-08-30 18:46:211855};
1856
Howard Hinnant99968442011-11-29 18:15:501857template<class _Rp, class ..._ArgTypes>
Howard Hinnant8bf01dd2012-07-21 17:46:551858__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211859{
1860 if (__f.__f_ == nullptr)
1861 __f_ = nullptr;
1862 else if (__f.__f_ == (__base*)&__f.__buf_)
1863 {
1864 __f_ = (__base*)&__buf_;
1865 __f.__f_->__move_to(__f_);
1866 }
1867 else
1868 {
1869 __f_ = __f.__f_;
1870 __f.__f_ = nullptr;
1871 }
1872}
1873
Howard Hinnant99968442011-11-29 18:15:501874template<class _Rp, class ..._ArgTypes>
1875template <class _Fp>
1876__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211877 : __f_(nullptr)
1878{
Marshall Clowf1264e72014-04-07 13:32:261879 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501880 typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211881 if (sizeof(_FF) <= sizeof(__buf_))
1882 {
1883 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501884 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211885 }
1886 else
1887 {
Howard Hinnant99968442011-11-29 18:15:501888 typedef allocator<_FF> _Ap;
1889 _Ap __a;
1890 typedef __allocator_destructor<_Ap> _Dp;
1891 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
1892 ::new (__hold.get()) _FF(_VSTD::forward<_Fp>(__f), allocator<_FR>(__a));
Howard Hinnant54da3382010-08-30 18:46:211893 __f_ = __hold.release();
1894 }
1895}
1896
Howard Hinnant99968442011-11-29 18:15:501897template<class _Rp, class ..._ArgTypes>
1898template <class _Fp, class _Alloc>
1899__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(
1900 allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:211901 : __f_(nullptr)
1902{
Marshall Clowf1264e72014-04-07 13:32:261903 typedef typename remove_reference<typename decay<_Fp>::type>::type _FR;
Howard Hinnant99968442011-11-29 18:15:501904 typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
Howard Hinnant54da3382010-08-30 18:46:211905 if (sizeof(_FF) <= sizeof(__buf_))
1906 {
1907 __f_ = (__base*)&__buf_;
Howard Hinnant99968442011-11-29 18:15:501908 ::new (__f_) _FF(_VSTD::forward<_Fp>(__f));
Howard Hinnant54da3382010-08-30 18:46:211909 }
1910 else
1911 {
Eric Fiselier4d2413c2014-10-23 06:24:451912 typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
Howard Hinnant99968442011-11-29 18:15:501913 _Ap __a(__a0);
1914 typedef __allocator_destructor<_Ap> _Dp;
1915 unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
Eric Fiselier4d2413c2014-10-23 06:24:451916 ::new (static_cast<void*>(_VSTD::addressof(*__hold.get())))
1917 _FF(_VSTD::forward<_Fp>(__f), _Alloc(__a));
1918 __f_ = _VSTD::addressof(*__hold.release());
Howard Hinnant54da3382010-08-30 18:46:211919 }
1920}
1921
Howard Hinnant99968442011-11-29 18:15:501922template<class _Rp, class ..._ArgTypes>
1923__packaged_task_function<_Rp(_ArgTypes...)>&
Howard Hinnant8bf01dd2012-07-21 17:46:551924__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211925{
1926 if (__f_ == (__base*)&__buf_)
1927 __f_->destroy();
1928 else if (__f_)
1929 __f_->destroy_deallocate();
1930 __f_ = nullptr;
1931 if (__f.__f_ == nullptr)
1932 __f_ = nullptr;
1933 else if (__f.__f_ == (__base*)&__f.__buf_)
1934 {
1935 __f_ = (__base*)&__buf_;
1936 __f.__f_->__move_to(__f_);
1937 }
1938 else
1939 {
1940 __f_ = __f.__f_;
1941 __f.__f_ = nullptr;
1942 }
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:451943 return *this;
Howard Hinnant54da3382010-08-30 18:46:211944}
1945
Howard Hinnant99968442011-11-29 18:15:501946template<class _Rp, class ..._ArgTypes>
1947__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function()
Howard Hinnant54da3382010-08-30 18:46:211948{
1949 if (__f_ == (__base*)&__buf_)
1950 __f_->destroy();
1951 else if (__f_)
1952 __f_->destroy_deallocate();
1953}
1954
Howard Hinnant99968442011-11-29 18:15:501955template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:211956void
Howard Hinnant8bf01dd2012-07-21 17:46:551957__packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:211958{
1959 if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_)
1960 {
1961 typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
1962 __base* __t = (__base*)&__tempbuf;
1963 __f_->__move_to(__t);
1964 __f_->destroy();
1965 __f_ = nullptr;
1966 __f.__f_->__move_to((__base*)&__buf_);
1967 __f.__f_->destroy();
1968 __f.__f_ = nullptr;
1969 __f_ = (__base*)&__buf_;
1970 __t->__move_to((__base*)&__f.__buf_);
1971 __t->destroy();
1972 __f.__f_ = (__base*)&__f.__buf_;
1973 }
1974 else if (__f_ == (__base*)&__buf_)
1975 {
1976 __f_->__move_to((__base*)&__f.__buf_);
1977 __f_->destroy();
1978 __f_ = __f.__f_;
1979 __f.__f_ = (__base*)&__f.__buf_;
1980 }
1981 else if (__f.__f_ == (__base*)&__f.__buf_)
1982 {
1983 __f.__f_->__move_to((__base*)&__buf_);
1984 __f.__f_->destroy();
1985 __f.__f_ = __f_;
1986 __f_ = (__base*)&__buf_;
1987 }
1988 else
Howard Hinnant0949eed2011-06-30 21:18:191989 _VSTD::swap(__f_, __f.__f_);
Howard Hinnant54da3382010-08-30 18:46:211990}
1991
Howard Hinnant99968442011-11-29 18:15:501992template<class _Rp, class ..._ArgTypes>
Evgeniy Stepanova3b25f82015-11-07 01:22:131993inline
Howard Hinnant99968442011-11-29 18:15:501994_Rp
1995__packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
Howard Hinnant54da3382010-08-30 18:46:211996{
Howard Hinnant0949eed2011-06-30 21:18:191997 return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
Howard Hinnant54da3382010-08-30 18:46:211998}
1999
Howard Hinnant99968442011-11-29 18:15:502000template<class _Rp, class ..._ArgTypes>
Eric Fiselierc3589a82017-01-04 23:56:002001class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212002{
2003public:
Eric Fiselier68db6cd2016-06-01 21:05:532004 typedef _Rp result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:212005
2006private:
2007 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2008 promise<result_type> __p_;
2009
2010public:
2011 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262012 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552013 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172014 template <class _Fp,
2015 class = typename enable_if
2016 <
2017 !is_same<
2018 typename decay<_Fp>::type,
2019 packaged_task
2020 >::value
2021 >::type
2022 >
Howard Hinnant8c6cbb22010-09-22 14:16:262023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502024 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172025 template <class _Fp, class _Allocator,
2026 class = typename enable_if
2027 <
2028 !is_same<
2029 typename decay<_Fp>::type,
2030 packaged_task
2031 >::value
2032 >::type
2033 >
Howard Hinnant8c6cbb22010-09-22 14:16:262034 _LIBCPP_INLINE_VISIBILITY
Marshall Clow07546f32015-06-30 14:16:492035 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502036 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212037 __p_(allocator_arg, __a) {}
2038 // ~packaged_task() = default;
2039
2040 // no copy
Howard Hinnant8131a012012-07-21 19:34:122041 packaged_task(const packaged_task&) = delete;
2042 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212043
2044 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552046 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192047 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552049 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212050 {
Howard Hinnant0949eed2011-06-30 21:18:192051 __f_ = _VSTD::move(__other.__f_);
2052 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212053 return *this;
2054 }
Howard Hinnant8c6cbb22010-09-22 14:16:262055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552056 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212057 {
2058 __f_.swap(__other.__f_);
2059 __p_.swap(__other.__p_);
2060 }
2061
Howard Hinnant8c6cbb22010-09-22 14:16:262062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552063 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212064
2065 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212067 future<result_type> get_future() {return __p_.get_future();}
2068
2069 // execution
2070 void operator()(_ArgTypes... __args);
2071 void make_ready_at_thread_exit(_ArgTypes... __args);
2072
2073 void reset();
2074};
2075
Howard Hinnant99968442011-11-29 18:15:502076template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212077void
Howard Hinnant99968442011-11-29 18:15:502078packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212079{
Howard Hinnant54da3382010-08-30 18:46:212080 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152081 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212082 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152083 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322084#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212085 try
2086 {
2087#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192088 __p_.set_value(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212089#ifndef _LIBCPP_NO_EXCEPTIONS
2090 }
2091 catch (...)
2092 {
2093 __p_.set_exception(current_exception());
2094 }
2095#endif // _LIBCPP_NO_EXCEPTIONS
2096}
2097
Howard Hinnant99968442011-11-29 18:15:502098template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212099void
Howard Hinnant99968442011-11-29 18:15:502100packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
Howard Hinnant54da3382010-08-30 18:46:212101{
Howard Hinnant54da3382010-08-30 18:46:212102 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152103 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212104 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152105 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322106#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212107 try
2108 {
2109#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192110 __p_.set_value_at_thread_exit(__f_(_VSTD::forward<_ArgTypes>(__args)...));
Howard Hinnant54da3382010-08-30 18:46:212111#ifndef _LIBCPP_NO_EXCEPTIONS
2112 }
2113 catch (...)
2114 {
2115 __p_.set_exception_at_thread_exit(current_exception());
2116 }
2117#endif // _LIBCPP_NO_EXCEPTIONS
2118}
2119
Howard Hinnant99968442011-11-29 18:15:502120template<class _Rp, class ..._ArgTypes>
Howard Hinnant54da3382010-08-30 18:46:212121void
Howard Hinnant99968442011-11-29 18:15:502122packaged_task<_Rp(_ArgTypes...)>::reset()
Howard Hinnant54da3382010-08-30 18:46:212123{
Howard Hinnant7de47902010-11-30 20:23:322124 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152125 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212126 __p_ = promise<result_type>();
2127}
2128
2129template<class ..._ArgTypes>
Eric Fiselierc3589a82017-01-04 23:56:002130class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)>
Howard Hinnant54da3382010-08-30 18:46:212131{
2132public:
Eric Fiselier68db6cd2016-06-01 21:05:532133 typedef void result_type; // extension
Howard Hinnant54da3382010-08-30 18:46:212134
2135private:
2136 __packaged_task_function<result_type(_ArgTypes...)> __f_;
2137 promise<result_type> __p_;
2138
2139public:
2140 // construction and destruction
Howard Hinnant8c6cbb22010-09-22 14:16:262141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552142 packaged_task() _NOEXCEPT : __p_(nullptr) {}
Marshall Clow5f2d5b92013-10-12 22:49:172143 template <class _Fp,
2144 class = typename enable_if
2145 <
2146 !is_same<
2147 typename decay<_Fp>::type,
2148 packaged_task
2149 >::value
2150 >::type
2151 >
Howard Hinnant8c6cbb22010-09-22 14:16:262152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502153 explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
Marshall Clow5f2d5b92013-10-12 22:49:172154 template <class _Fp, class _Allocator,
2155 class = typename enable_if
2156 <
2157 !is_same<
2158 typename decay<_Fp>::type,
2159 packaged_task
2160 >::value
2161 >::type
2162 >
Howard Hinnant8c6cbb22010-09-22 14:16:262163 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5706c372015-06-30 18:28:352164 packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
Howard Hinnant99968442011-11-29 18:15:502165 : __f_(allocator_arg, __a, _VSTD::forward<_Fp>(__f)),
Howard Hinnant54da3382010-08-30 18:46:212166 __p_(allocator_arg, __a) {}
2167 // ~packaged_task() = default;
2168
2169 // no copy
Howard Hinnant8131a012012-07-21 19:34:122170 packaged_task(const packaged_task&) = delete;
2171 packaged_task& operator=(const packaged_task&) = delete;
Howard Hinnant54da3382010-08-30 18:46:212172
2173 // move support
Howard Hinnant8c6cbb22010-09-22 14:16:262174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552175 packaged_task(packaged_task&& __other) _NOEXCEPT
Howard Hinnant0949eed2011-06-30 21:18:192176 : __f_(_VSTD::move(__other.__f_)), __p_(_VSTD::move(__other.__p_)) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552178 packaged_task& operator=(packaged_task&& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212179 {
Howard Hinnant0949eed2011-06-30 21:18:192180 __f_ = _VSTD::move(__other.__f_);
2181 __p_ = _VSTD::move(__other.__p_);
Howard Hinnant54da3382010-08-30 18:46:212182 return *this;
2183 }
Howard Hinnant8c6cbb22010-09-22 14:16:262184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552185 void swap(packaged_task& __other) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212186 {
2187 __f_.swap(__other.__f_);
2188 __p_.swap(__other.__p_);
2189 }
2190
Howard Hinnant8c6cbb22010-09-22 14:16:262191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552192 bool valid() const _NOEXCEPT {return __p_.__state_ != nullptr;}
Howard Hinnant54da3382010-08-30 18:46:212193
2194 // result retrieval
Howard Hinnant8c6cbb22010-09-22 14:16:262195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant54da3382010-08-30 18:46:212196 future<result_type> get_future() {return __p_.get_future();}
2197
2198 // execution
2199 void operator()(_ArgTypes... __args);
2200 void make_ready_at_thread_exit(_ArgTypes... __args);
2201
2202 void reset();
2203};
2204
2205template<class ..._ArgTypes>
2206void
2207packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args)
2208{
Howard Hinnant54da3382010-08-30 18:46:212209 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152210 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212211 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152212 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322213#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212214 try
2215 {
2216#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192217 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212218 __p_.set_value();
2219#ifndef _LIBCPP_NO_EXCEPTIONS
2220 }
2221 catch (...)
2222 {
2223 __p_.set_exception(current_exception());
2224 }
2225#endif // _LIBCPP_NO_EXCEPTIONS
2226}
2227
2228template<class ..._ArgTypes>
2229void
2230packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args)
2231{
Howard Hinnant54da3382010-08-30 18:46:212232 if (__p_.__state_ == nullptr)
Eric Fiselier423ca202015-10-02 21:25:152233 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212234 if (__p_.__state_->__has_value())
Eric Fiselier423ca202015-10-02 21:25:152235 __throw_future_error(future_errc::promise_already_satisfied);
Marshall Clowa1899742015-09-03 15:11:322236#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant54da3382010-08-30 18:46:212237 try
2238 {
2239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:192240 __f_(_VSTD::forward<_ArgTypes>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212241 __p_.set_value_at_thread_exit();
2242#ifndef _LIBCPP_NO_EXCEPTIONS
2243 }
2244 catch (...)
2245 {
2246 __p_.set_exception_at_thread_exit(current_exception());
2247 }
2248#endif // _LIBCPP_NO_EXCEPTIONS
2249}
2250
2251template<class ..._ArgTypes>
2252void
2253packaged_task<void(_ArgTypes...)>::reset()
2254{
Howard Hinnant7de47902010-11-30 20:23:322255 if (!valid())
Eric Fiselier423ca202015-10-02 21:25:152256 __throw_future_error(future_errc::no_state);
Howard Hinnant54da3382010-08-30 18:46:212257 __p_ = promise<result_type>();
2258}
2259
2260template <class _Callable>
2261inline _LIBCPP_INLINE_VISIBILITY
2262void
Howard Hinnant8bf01dd2012-07-21 17:46:552263swap(packaged_task<_Callable>& __x, packaged_task<_Callable>& __y) _NOEXCEPT
Howard Hinnant54da3382010-08-30 18:46:212264{
2265 __x.swap(__y);
2266}
2267
2268template <class _Callable, class _Alloc>
Eric Fiselierc3589a82017-01-04 23:56:002269struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc>
Howard Hinnant8c6cbb22010-09-22 14:16:262270 : public true_type {};
Howard Hinnant54da3382010-08-30 18:46:212271
Howard Hinnant99968442011-11-29 18:15:502272template <class _Rp, class _Fp>
2273future<_Rp>
Howard Hinnant73d21a42010-09-04 23:28:192274#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502275__make_deferred_assoc_state(_Fp&& __f)
Howard Hinnant54da3382010-08-30 18:46:212276#else
Howard Hinnant99968442011-11-29 18:15:502277__make_deferred_assoc_state(_Fp __f)
Howard Hinnant54da3382010-08-30 18:46:212278#endif
2279{
Howard Hinnant99968442011-11-29 18:15:502280 unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count>
2281 __h(new __deferred_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2282 return future<_Rp>(__h.get());
Howard Hinnant54da3382010-08-30 18:46:212283}
2284
Howard Hinnant99968442011-11-29 18:15:502285template <class _Rp, class _Fp>
2286future<_Rp>
Howard Hinnant57cff292011-05-19 15:05:042287#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99968442011-11-29 18:15:502288__make_async_assoc_state(_Fp&& __f)
Howard Hinnant57cff292011-05-19 15:05:042289#else
Howard Hinnant99968442011-11-29 18:15:502290__make_async_assoc_state(_Fp __f)
Howard Hinnant57cff292011-05-19 15:05:042291#endif
2292{
Howard Hinnant99968442011-11-29 18:15:502293 unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count>
2294 __h(new __async_assoc_state<_Rp, _Fp>(_VSTD::forward<_Fp>(__f)));
2295 _VSTD::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
2296 return future<_Rp>(__h.get());
Howard Hinnant57cff292011-05-19 15:05:042297}
2298
Howard Hinnant99968442011-11-29 18:15:502299template <class _Fp, class... _Args>
Howard Hinnant57cff292011-05-19 15:05:042300class __async_func
2301{
Howard Hinnant99968442011-11-29 18:15:502302 tuple<_Fp, _Args...> __f_;
Howard Hinnant57cff292011-05-19 15:05:042303
2304public:
Howard Hinnant99968442011-11-29 18:15:502305 typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
Howard Hinnant57cff292011-05-19 15:05:042306
2307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502308 explicit __async_func(_Fp&& __f, _Args&&... __args)
Howard Hinnant0949eed2011-06-30 21:18:192309 : __f_(_VSTD::move(__f), _VSTD::move(__args)...) {}
Howard Hinnant57cff292011-05-19 15:05:042310
2311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0949eed2011-06-30 21:18:192312 __async_func(__async_func&& __f) : __f_(_VSTD::move(__f.__f_)) {}
Howard Hinnant57cff292011-05-19 15:05:042313
Howard Hinnant99968442011-11-29 18:15:502314 _Rp operator()()
Howard Hinnant57cff292011-05-19 15:05:042315 {
2316 typedef typename __make_tuple_indices<1+sizeof...(_Args), 1>::type _Index;
2317 return __execute(_Index());
2318 }
2319private:
2320 template <size_t ..._Indices>
Howard Hinnant99968442011-11-29 18:15:502321 _Rp
Howard Hinnant57cff292011-05-19 15:05:042322 __execute(__tuple_indices<_Indices...>)
2323 {
Howard Hinnant0949eed2011-06-30 21:18:192324 return __invoke(_VSTD::move(_VSTD::get<0>(__f_)), _VSTD::move(_VSTD::get<_Indices>(__f_))...);
Howard Hinnant57cff292011-05-19 15:05:042325 }
2326};
2327
Marshall Clow3b3108e2013-11-03 22:06:532328inline _LIBCPP_INLINE_VISIBILITY bool __does_policy_contain(launch __policy, launch __value )
Marshall Clowad2a6002013-11-03 15:43:352329{ return (int(__policy) & int(__value)) != 0; }
2330
Howard Hinnant99968442011-11-29 18:15:502331template <class _Fp, class... _Args>
2332future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2333async(launch __policy, _Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212334{
Howard Hinnant99968442011-11-29 18:15:502335 typedef __async_func<typename decay<_Fp>::type, typename decay<_Args>::type...> _BF;
2336 typedef typename _BF::_Rp _Rp;
Marshall Clowad2a6002013-11-03 15:43:352337
2338#ifndef _LIBCPP_NO_EXCEPTIONS
2339 try
2340 {
2341#endif
2342 if (__does_policy_contain(__policy, launch::async))
2343 return _VSTD::__make_async_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192344 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352345#ifndef _LIBCPP_NO_EXCEPTIONS
2346 }
2347 catch ( ... ) { if (__policy == launch::async) throw ; }
2348#endif
2349
2350 if (__does_policy_contain(__policy, launch::deferred))
2351 return _VSTD::__make_deferred_assoc_state<_Rp>(_BF(__decay_copy(_VSTD::forward<_Fp>(__f)),
Howard Hinnant0949eed2011-06-30 21:18:192352 __decay_copy(_VSTD::forward<_Args>(__args))...));
Marshall Clowad2a6002013-11-03 15:43:352353 return future<_Rp>{};
Howard Hinnant54da3382010-08-30 18:46:212354}
2355
Howard Hinnant99968442011-11-29 18:15:502356template <class _Fp, class... _Args>
Howard Hinnant54da3382010-08-30 18:46:212357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502358future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
2359async(_Fp&& __f, _Args&&... __args)
Howard Hinnant54da3382010-08-30 18:46:212360{
Howard Hinnant99968442011-11-29 18:15:502361 return _VSTD::async(launch::any, _VSTD::forward<_Fp>(__f),
Howard Hinnant0949eed2011-06-30 21:18:192362 _VSTD::forward<_Args>(__args)...);
Howard Hinnant54da3382010-08-30 18:46:212363}
2364
2365#endif // _LIBCPP_HAS_NO_VARIADICS
2366
Howard Hinnante6e4d012010-09-03 21:46:372367// shared_future
2368
Howard Hinnant99968442011-11-29 18:15:502369template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:002370class _LIBCPP_TEMPLATE_VIS shared_future
Howard Hinnant99be8232010-09-03 18:39:252371{
Howard Hinnant99968442011-11-29 18:15:502372 __assoc_state<_Rp>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252373
2374public:
Howard Hinnant8c6cbb22010-09-22 14:16:262375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552376 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262377 _LIBCPP_INLINE_VISIBILITY
Marshall Clow3d7c49b2016-11-14 19:58:052378 shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252379 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192380#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552382 shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252383 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552385 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252386 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192387#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252388 ~shared_future();
Marshall Clow3d7c49b2016-11-14 19:58:052389 shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
Howard Hinnant73d21a42010-09-04 23:28:192390#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552392 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252393 {
2394 shared_future(std::move(__rhs)).swap(*this);
2395 return *this;
2396 }
Howard Hinnant73d21a42010-09-04 23:28:192397#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252398
2399 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502401 const _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252402
Howard Hinnant8c6cbb22010-09-22 14:16:262403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552404 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252405
2406 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552408 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252409
Howard Hinnant8c6cbb22010-09-22 14:16:262410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252411 void wait() const {__state_->wait();}
2412 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262413 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252414 future_status
2415 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2416 {return __state_->wait_for(__rel_time);}
2417 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262418 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252419 future_status
2420 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2421 {return __state_->wait_until(__abs_time);}
2422};
2423
Howard Hinnant99968442011-11-29 18:15:502424template <class _Rp>
2425shared_future<_Rp>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252426{
2427 if (__state_)
2428 __state_->__release_shared();
2429}
2430
Howard Hinnant99968442011-11-29 18:15:502431template <class _Rp>
2432shared_future<_Rp>&
Marshall Clow3d7c49b2016-11-14 19:58:052433shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252434{
2435 if (__rhs.__state_)
2436 __rhs.__state_->__add_shared();
2437 if (__state_)
2438 __state_->__release_shared();
2439 __state_ = __rhs.__state_;
2440 return *this;
2441}
2442
Howard Hinnant99968442011-11-29 18:15:502443template <class _Rp>
Eric Fiselierc3589a82017-01-04 23:56:002444class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&>
Howard Hinnant99be8232010-09-03 18:39:252445{
Howard Hinnant99968442011-11-29 18:15:502446 __assoc_state<_Rp&>* __state_;
Howard Hinnant99be8232010-09-03 18:39:252447
2448public:
Howard Hinnant8c6cbb22010-09-22 14:16:262449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552450 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252452 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2453 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192454#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552456 shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252457 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552459 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252460 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192461#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252462 ~shared_future();
2463 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192464#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262465 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552466 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252467 {
2468 shared_future(std::move(__rhs)).swap(*this);
2469 return *this;
2470 }
Howard Hinnant73d21a42010-09-04 23:28:192471#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252472
2473 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262474 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:502475 _Rp& get() const {return __state_->copy();}
Howard Hinnant99be8232010-09-03 18:39:252476
Howard Hinnant8c6cbb22010-09-22 14:16:262477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552478 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252479
2480 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552482 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252483
Howard Hinnant8c6cbb22010-09-22 14:16:262484 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252485 void wait() const {__state_->wait();}
2486 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252488 future_status
2489 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2490 {return __state_->wait_for(__rel_time);}
2491 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252493 future_status
2494 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2495 {return __state_->wait_until(__abs_time);}
2496};
2497
Howard Hinnant99968442011-11-29 18:15:502498template <class _Rp>
2499shared_future<_Rp&>::~shared_future()
Howard Hinnant99be8232010-09-03 18:39:252500{
2501 if (__state_)
2502 __state_->__release_shared();
2503}
2504
Howard Hinnant99968442011-11-29 18:15:502505template <class _Rp>
2506shared_future<_Rp&>&
2507shared_future<_Rp&>::operator=(const shared_future& __rhs)
Howard Hinnant99be8232010-09-03 18:39:252508{
2509 if (__rhs.__state_)
2510 __rhs.__state_->__add_shared();
2511 if (__state_)
2512 __state_->__release_shared();
2513 __state_ = __rhs.__state_;
2514 return *this;
2515}
2516
2517template <>
Howard Hinnant83eade62013-03-06 23:30:192518class _LIBCPP_TYPE_VIS shared_future<void>
Howard Hinnant99be8232010-09-03 18:39:252519{
2520 __assoc_sub_state* __state_;
2521
2522public:
Howard Hinnant8c6cbb22010-09-22 14:16:262523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552524 shared_future() _NOEXCEPT : __state_(nullptr) {}
Howard Hinnant8c6cbb22010-09-22 14:16:262525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252526 shared_future(const shared_future& __rhs) : __state_(__rhs.__state_)
2527 {if (__state_) __state_->__add_shared();}
Howard Hinnant73d21a42010-09-04 23:28:192528#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552530 shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_)
Howard Hinnant99be8232010-09-03 18:39:252531 {__f.__state_ = nullptr;}
Howard Hinnant8c6cbb22010-09-22 14:16:262532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552533 shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_)
Howard Hinnant99be8232010-09-03 18:39:252534 {__rhs.__state_ = nullptr;}
Howard Hinnant73d21a42010-09-04 23:28:192535#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252536 ~shared_future();
2537 shared_future& operator=(const shared_future& __rhs);
Howard Hinnant73d21a42010-09-04 23:28:192538#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant8c6cbb22010-09-22 14:16:262539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552540 shared_future& operator=(shared_future&& __rhs) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252541 {
2542 shared_future(std::move(__rhs)).swap(*this);
2543 return *this;
2544 }
Howard Hinnant73d21a42010-09-04 23:28:192545#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant99be8232010-09-03 18:39:252546
2547 // retrieving the value
Howard Hinnant8c6cbb22010-09-22 14:16:262548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252549 void get() const {__state_->copy();}
2550
Howard Hinnant8c6cbb22010-09-22 14:16:262551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552552 void swap(shared_future& __rhs) _NOEXCEPT {_VSTD::swap(__state_, __rhs.__state_);}
Howard Hinnant99be8232010-09-03 18:39:252553
2554 // functions to check state
Howard Hinnant8c6cbb22010-09-22 14:16:262555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8bf01dd2012-07-21 17:46:552556 bool valid() const _NOEXCEPT {return __state_ != nullptr;}
Howard Hinnant99be8232010-09-03 18:39:252557
Howard Hinnant8c6cbb22010-09-22 14:16:262558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252559 void wait() const {__state_->wait();}
2560 template <class _Rep, class _Period>
Howard Hinnant8c6cbb22010-09-22 14:16:262561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252562 future_status
2563 wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const
2564 {return __state_->wait_for(__rel_time);}
2565 template <class _Clock, class _Duration>
Howard Hinnant8c6cbb22010-09-22 14:16:262566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99be8232010-09-03 18:39:252567 future_status
2568 wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const
2569 {return __state_->wait_until(__abs_time);}
2570};
2571
Howard Hinnant99968442011-11-29 18:15:502572template <class _Rp>
Howard Hinnant99be8232010-09-03 18:39:252573inline _LIBCPP_INLINE_VISIBILITY
2574void
Howard Hinnant8bf01dd2012-07-21 17:46:552575swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT
Howard Hinnant99be8232010-09-03 18:39:252576{
2577 __x.swap(__y);
2578}
2579
Howard Hinnant99968442011-11-29 18:15:502580template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132581inline
Howard Hinnant99968442011-11-29 18:15:502582shared_future<_Rp>
Marshall Clow9bb0cca2017-01-24 23:28:252583future<_Rp>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:372584{
Howard Hinnant99968442011-11-29 18:15:502585 return shared_future<_Rp>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372586}
2587
Howard Hinnant99968442011-11-29 18:15:502588template <class _Rp>
Evgeniy Stepanova3b25f82015-11-07 01:22:132589inline
Howard Hinnant99968442011-11-29 18:15:502590shared_future<_Rp&>
Marshall Clow9bb0cca2017-01-24 23:28:252591future<_Rp&>::share() _NOEXCEPT
Howard Hinnante6e4d012010-09-03 21:46:372592{
Howard Hinnant99968442011-11-29 18:15:502593 return shared_future<_Rp&>(_VSTD::move(*this));
Howard Hinnant7de47902010-11-30 20:23:322594}
2595
Howard Hinnanta4451512010-12-02 16:45:212596#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2597
Evgeniy Stepanova3b25f82015-11-07 01:22:132598inline
Howard Hinnant7de47902010-11-30 20:23:322599shared_future<void>
Marshall Clow9bb0cca2017-01-24 23:28:252600future<void>::share() _NOEXCEPT
Howard Hinnant7de47902010-11-30 20:23:322601{
Howard Hinnant0949eed2011-06-30 21:18:192602 return shared_future<void>(_VSTD::move(*this));
Howard Hinnante6e4d012010-09-03 21:46:372603}
2604
Howard Hinnanta4451512010-12-02 16:45:212605#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2606
Howard Hinnantbc8d3f92010-05-11 19:42:162607_LIBCPP_END_NAMESPACE_STD
2608
Jonathan Roelofs8d86b2e2014-09-05 19:45:052609#endif // !_LIBCPP_HAS_NO_THREADS
2610
Howard Hinnantbc8d3f92010-05-11 19:42:162611#endif // _LIBCPP_FUTURE